package com.roy.DubboRPCDemo;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.roy.DubboRPCDemo.bean.RPCResBean;
import com.roy.DubboRPCDemo.bean.UserBean;
import com.roy.DubboRPCDemo.protocol.MyService;

public class RPCClient {

	public static void main(String[] args) {
		long starttime = (new Date()).getTime();
		ReferenceConfig<MyService> referenceConfig = new ReferenceConfig<MyService>();
		
		ApplicationConfig applicationConfig = new ApplicationConfig("MyFirstDubboConsumer");
		applicationConfig.setQosEnable(false);
		applicationConfig.setQosAcceptForeignIp(false);
		applicationConfig.setQosPort(20882);
        referenceConfig.setApplication(applicationConfig);
        
//        RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181?client=zkclient");
        RegistryConfig registryConfig = new RegistryConfig("multicast://224.5.6.7:1234");
        referenceConfig.setRegistry(registryConfig);
        referenceConfig.setInterface(MyService.class);
        //官方建议，这个service初始化之后立即缓存起来，可以像用本地对象一样使用。
        MyService service = referenceConfig.get();
        System.out.println("初始化proxy耗时："+((new Date()).getTime() - starttime));
        
        Properties properties = System.getProperties();
        properties.setProperty("dubbo.qos.port", "20881");
        String result = service.sayHello("world");
        System.out.println(result);
//        System.out.println("基础字段类型服务调用");
//		for(int i = 0 ; i < 10; i ++) {
//			starttime = (new Date()).getTime();
//			System.out.println(service.baseTypeService(100, "baseTypeUser", "123123123"));
//			System.out.println("服务调用耗时："+((new Date()).getTime() - starttime));
//		}
//		try {
//			Thread.sleep(1000);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		System.out.println("Map类型服务调用");
//		for(int i = 0 ; i < 10; i ++) {
//			starttime = (new Date()).getTime();
//			Map<String,String> userInfo = new HashMap<>();
//			userInfo.put("userId", ""+i);
//			userInfo.put("userName", "UserName : "+i);
//			userInfo.put("userPassword", "UserPassword : "+i);
//			System.out.println(service.mapTypeService(userInfo));
//			System.out.println("服务调用耗时："+((new Date()).getTime() - starttime));
//		}
//		System.out.println("对象类型服务调用");
//		for(int i = 0 ; i < 10; i ++) {
//			starttime = (new Date()).getTime();
//			UserBean user = new UserBean();
//			user.setUserId(i);
//			user.setUserName("UserName:"+i);
//			user.setUserPassword("UserPassword:"+i);
//			System.out.println(service.objTypeService(user));
//			System.out.println("服务调用耗时："+((new Date()).getTime() - starttime));
//		}
//		System.out.println("对象数组类型服务调用");
//		starttime = (new Date()).getTime();
//		List<UserBean> users = new ArrayList<UserBean>();
//		for(int i = 0 ; i < 10; i ++) {
//			UserBean user = new UserBean();
//			user.setUserId(i);
//			user.setUserName("UserName:"+i);
//			user.setUserPassword("UserPassword:"+i);
//			users.add(user);
//		}
//		List<UserBean> userBeanArray = service.objArrayService(users);
//		System.out.println(userBeanArray);
//		System.out.println("服务调用耗时："+((new Date()).getTime() - starttime));
		
		System.out.println("框架类型服务调用");
		
		System.out.println("调用initUser服务");
		starttime = (new Date()).getTime();
		String[] params = new String[] {"101","usreName"};
		RPCResBean initUserRes = service.frameworkService("initUser", params);
		System.out.println("调用服务initUser完成,res= "+initUserRes);
		System.out.println("服务调用耗时："+((new Date()).getTime() - starttime));
		
//		System.out.println("调用destroyUser服务");
//		starttime = (new Date()).getTime();
//		params = new String[] {"102"};
//		RPCResBean desUserRes = service.frameworkService("destroyUser", params);
//		System.out.println("调用服务destroyUser完成,res= "+desUserRes);
//		System.out.println("服务调用耗时："+((new Date()).getTime() - starttime));
        
	}
}
